title 'JOP1.sas, Analyses for Hensel-Goertz-Diehl JOP paper Tables 1-3';
options linesize=80 pagesize=60;

/*Note that users will need to update this file to list the correct location
of the SAS data sets to be used; this file currently uses the directory on Paul Hensel's
computer, and thus will not function on other computers unless this is corrected*/

/*Read in files to be used*/
data ersumgd;
  infile 'Hensel:Data:ersumgd.csv' delimiter=',';
  input gdnumber dyad begriv endriv evrivgd gdrivdur gdrivmid gdrivwar
    chgdix chgdixa demchg demchga demdix2b demdix2e;
proc sort;
  by evrivgd gdnumber;
run;

data erdemyr;
  infile 'Hensel:Data:erdemyr.csv' delimiter=',';
  input gdnumber dyad year evrivgd demdix2 chgdix chgdixa demchg demchga
    midyr afteryr rivbegyr rivendyr rivdecad aftdecad;
if afteryr>-1;
demdixon=0;
  if demdix2=2 then demdixon=1;
proc sort;
  by evrivgd gdnumber;
run;


/*Analyses to be run*/
data ersumgd;
  set ersumgd;
title 'Table 1, Dyadic Regime Type at Rivalry Onset';
title2 '(calculate chi-square statistics for entire table by hand)';
proc freq;  tables evrivgd*demdix2b;
run;

title 'Table 1 supplement A, Regime Type at Rivalry Onset and Rivalry Duration';
title2 '(demdix2b: 2=joint dem, 1=dem-other, 0=neither dem)';
proc anova;  class demdix2b;  model gdrivdur = demdix2b;  means demdix2b;
run;

title 'Table 1 supplement B, Regime Type at Rivalry Onset and Rivalry MIDs';
title2 '(demdix2b: 2=joint dem, 1=dem-other, 0=neither dem)';
proc anova;  class demdix2b;  model gdrivmid = demdix2b;  means demdix2b;
run;

title 'Table 2, Dyadic Regime Change Status during Interstate Rivalries';
title2 '(chgdix: 2=always joint dem, 1=sometimes, 0=never joint dem)';
proc freq;  tables chgdix*evrivgd;
run;

title 'Table 2 supplement A1, Regime Change and Rivalry Duration';
title2 '(chgdix: 2=always joint dem, 1=sometimes, 0=never joint dem)';
proc anova;  class chgdix;  model gdrivdur = chgdix;  means chgdix;
run;

title 'Table 2 supplement B1, Regime Change and Rivalry MIDs';
title2 '(chgdix: 2=always joint dem, 1=sometimes, 0=never joint dem)';
proc anova;  class chgdix;  model gdrivmid = chgdix;  means chgdix;
run;

title 'Table 2 supplement A2, Regime Change and Rivalry Duration - PR/ER Only';
title2 '(chgdix: 2=always joint dem, 1=sometimes, 0=never joint dem)';
proc anova;  class chgdix;  model gdrivdur = chgdix;  means chgdix;
  where evrivgd>1;
run;

title 'Table 2 supplement B2, Regime Change and Rivalry MIDs - PR/ER Only';
title2 '(chgdix: 2=always joint dem, 1=sometimes, 0=never joint dem)';
proc anova;  class chgdix;  model gdrivmid = chgdix;  means chgdix;
  where evrivgd>1;
run;

data ersum1;
  set ersumgd;
keep gdnumber begriv;
proc sort;
  by gdnumber;

data erdemyr;
  set erdemyr;
proc sort;
  by gdnumber;

data work;
  merge erdemyr(in=a) ersum1;
  by gdnumber;
begrivyr=int(begriv/10000);
first20=.;
  if begrivyr le year le (begrivyr+19) then first20=1;
  if year>(begrivyr+19) then first20=0;
run;

/*Note that the following analyses include the full period of militarized
rivalry - from the beginning of the first MID to the end of the last - as well as
the fifteen years after the final MID because a new MID in these years would have
had the effect of continuing the rivalry*/

title 'Table 3, MID Propensity (regime chg proto-rivalries only)';
title2 '(including entire period of rivalry+15 years after last MID)';
proc sort;
  by evrivgd;
proc freq;
  tables demdix2*midyr / chisq;
  by evrivgd;
  where demchg=1 and 16>afteryr>-1 and evrivgd=2;
run;
title 'Table 3, MID Propensity (regime chg enduring rivalries only)';
title2 '(including entire period of rivalry+15 years after last MID)';
proc freq;
  tables demdix2*midyr / chisq;
  by evrivgd;
  where demchg=1 and 16>afteryr>-1 and evrivgd=3;
run;
title 'Table 3, MID Propensity (regime chg proto- and enduring rivalries)';
title2 '(including entire period of rivalry+15 years after last MID)';
proc freq;
  tables demdix2*midyr / chisq;
  where demchg=1 and 16>afteryr>-1 and evrivgd>1;
run;

title 'Footnote 9, MID Propensity (regime chg enduring rivalries)';
title2 '(first20=1: first 20 yrs of rivalry / first20=0: all subsequent yrs)';
proc sort;
  by first20;
proc freq;
  tables demdix2*midyr / chisq;
  by first20;
  where demchg=1 and 16>afteryr>-1 and evrivgd=3;
run;

title 'Footnote 9, MID Propensity (regime chg enduring rivalries/joint-dem dummy)';
title2 '(first20=1: first 20 yrs of rivalry / first20=0: all subsequent yrs)';
proc sort;
  by first20;
proc freq;
  tables demdixon*midyr / chisq;
  by first20;
  where demchg=1 and 16>afteryr>-1 and evrivgd=3;
run;
